import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcrypt';

import {
    invoices,
    customers,
    revenues,
    users,
} from './placeholder-data.mjs';

const prisma = new PrismaClient()

async function seedUsers() {
    try {
        const insertedUsers = await Promise.all(
            users.map(async (user) => {
                const hashedPassword = await bcrypt.hash(user.password, 10);
                return prisma.user.upsert({
                    where: {
                        email: user.email
                    },
                    update: {},
                    create: {
                        name: user.name,
                        email: user.email,
                        password: hashedPassword
                    }
                })
            }),
        );

        console.log(`Seeded ${insertedUsers.length} users`);

        return {
            users: insertedUsers,
        };
    } catch (error) {
        console.error('Error seeding users:', error);
        throw error;
    }
}

async function seedCustomers() {
    const insertedCustomers = await Promise.all(
        customers.map(
            (customer) => {
                const customerInvoices = invoices.filter(invoice => invoice.customer_id === customer.id);

                return prisma.customer.upsert({
                    where: {
                        email: customer.email,
                    },
                    update: {},
                    create: {
                        name: customer.name,
                        email: customer.email,
                        image_url: customer.image_url,
                        invoices: {
                            create: customerInvoices.map(invoice => {
                                return {
                                    amount: invoice.amount,
                                    status: invoice.status,
                                    date: invoice.date
                                }
                            })
                        }
                    },
                })
            }
        )
    );
    console.log(`Seeded ${insertedCustomers.length} customers`);
    return {
        customers: insertedCustomers,
    }
}

async function seedRevenue() {
    try {

        const insertedRevenue = await Promise.all(
            revenues.map(
                (revenue) => prisma.revenue.upsert({
                    where: {
                        month: revenue.month,
                    },
                    update: {},
                    create: {
                        month: revenue.month,
                        revenue: revenue.revenue
                    }
                })
            ),
        );

        console.log(`Seeded ${insertedRevenue.length} revenue`);

        return {
            revenue: insertedRevenue,
        };
    } catch (error) {
        console.error('Error seeding revenue:', error);
        throw error;
    }
}

async function main() {
    await seedUsers();
    await seedCustomers();
    await seedRevenue();
}

main()
    .then(async () => {
        await prisma.$disconnect()
    })
    .catch(async (e) => {
        console.error(e)
        await prisma.$disconnect()
        process.exit(1)
    })